package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, err
	}
	if os.IsNotExist(err) {
		return false, err
	}
	return false, err
}

func copyFile(src, dst string) (int64, error) {
	srcFile, err := os.Open(src)
	if err != nil {
		fmt.Printf("open file err=%v", err)
		return 0, err
	}
	defer srcFile.Close()
	//创建缓存读取reader
	reader := bufio.NewReader(srcFile)
	dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		fmt.Printf("create file err=%v", err)
		return 0, err
	}
	//创建缓存写入writer
	writer := bufio.NewWriter(dstFile)
	defer dstFile.Close()
	return io.Copy(writer, reader)

}

func main() {
	fileName1 := "./IMG_0001.jpeg"
	fileName2 := "./IMG_0001_copy.jpeg"
	fExists, err := PathExists(fileName1)
	fmt.Println(fExists, err)
	fExists, err = PathExists(fileName2)
	fmt.Println(fExists, err)

	size, err := copyFile(fileName1, fileName2)
	fmt.Println(size, err)
}
